
#include "ControlDef.h"

//已删除全部注释

//CHSlider
//////////////////////////////////////////////////////////////////////////
CHSlider::CHSlider(WCHAR* Name,int X,int Y,int Width)
{
	Type=CT_Slider;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=SliderSize;

	BodyWidth=Width-SliderSize;
	BodyBegX=SliderSize/2;
	BodyEndX=Width-SliderSize/2;

	SetRange(0.0f,100.0f,0.0f);

	ImgSlider=ResMgr->LoadImageR(L"滑块Normal");
	ImgSliderDisable=ResMgr->LoadImageR(L"滑块Disable");
}

CHSlider::~CHSlider()
{
	ResMgr->ReleaseImageR(ImgSlider);
    ResMgr->ReleaseImageR(ImgSliderDisable);
	delete[] Name;
}

void CHSlider::SetRange(float MinValue,float MaxValue,float Step)
{
	if (MinValue<MaxValue && MaxValue-MinValue>=Step)
	{
		this->MinValue=MinValue;
		this->MaxValue=MaxValue;
		this->Step=Step;
		ValueRatio=0.0f;
		Value=MinValue;
		SliderPosX=0;
	}
}

void CHSlider::SetValue(float NewValue)
{
	if (Step!=0)
	{
		if (NewValue>=0)
		{
			float Remainder=fmod(NewValue,Step);
			if (Remainder<Step*0.5f)
				NewValue=NewValue-Remainder;
			else
				NewValue=NewValue-Remainder+Step;
		}
		else
		{
			float Remainder=fmod(NewValue,Step);
			float AbsRemainder=fabs(Remainder);
			if (AbsRemainder<Step*0.5f)
				NewValue=NewValue-Remainder;
			else
				NewValue=NewValue-Remainder-Step;
		}
	}
	if (Value==NewValue)
		return ;
	if (NewValue<MinValue || NewValue>MaxValue)
		return ;

	Value=NewValue;
	ValueRatio=(Value-MinValue)/(MaxValue-MinValue);
	
	if (!Pushed)
		SliderPosX=ValueRatio*BodyWidth;

	if (EventHandler!=NULL)
		EventHandler(this,EvtSliderChange,(PVOID)Mul1000(NewValue),(PVOID)TRUE);
}

float CHSlider::GetValue()
{
	return Value;
}

void CHSlider::SetSliderPos(int NewPos)
{
	if (SliderPosX==NewPos)
		return ;
	SliderPosX=NewPos;
	float NewValue=(MaxValue-MinValue)*SliderPosX/BodyWidth+MinValue;
	SetValue(NewValue);
}

void CHSlider::Draw()
{
	int SlotPosX=SliderSize/2;
	int SlotPosY=(SliderSize-SlotHeight)/2;
	int SlotWidth=Width-SliderSize/2*2;

	DWORD SlotColor=0xFFF0B000;
	ImageR_t* ImgDraw=ImgSlider;
	if (!Enable)
	{
		SlotColor=0xFFB1A88F;
		ImgDraw=ImgSliderDisable;
	}
	D2dMgr->SetColor(SlotColor);
	D2dMgr->FillRoundRect(SlotPosX,SlotPosY,SlotWidth,SlotHeight,1);
	D2dMgr->DrawImage(SliderPosX,0,ImgDraw);

#ifdef _DEBUG
	D2dMgr->SetColor(0xFF000000);
	D2dMgr->DrawRectangle(0,0,Width,Height);
#endif
}

int CHSlider::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	int MouseX=(int)Param1;
	int MouseY=(int)Param2;
	if (UIEvent==EvtMouseDown)
	{
		if (MouseX>=BodyBegX && MouseX<=BodyEndX)
			SetSliderPos(MouseX-BodyBegX);
	}
	else if (UIEvent==EvtMouseMove)
	{
		if (Pushed)
		{
			if (MouseX>=BodyBegX && MouseX<=BodyEndX)
				SetSliderPos(MouseX-BodyBegX);
			else if (MouseX<BodyBegX)
				SetSliderPos(0);
			else if (MouseX>BodyEndX)
				SetSliderPos(BodyWidth);
		}
	}
	else if (UIEvent==EvtMouseUp)
	{
		SliderPosX=ValueRatio*BodyWidth;
	}
	return 0;
}


//CHTabView
//////////////////////////////////////////////////////////////////////////
CHTabView::CHTabView(WCHAR* Name,int X,int Y,int Width,int Height)
{
	Type=CT_TabView;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;
	BodyHeight=Height-TabHeight;

	DisplayIndex=-1;

	FontText=ResMgr->LoadFontR(L"宋体-14");
	TabFullNormal=ResMgr->LoadScale9R(L"Tab全_Normal");
	TabLeftNormal=ResMgr->LoadScale9R(L"Tab左_Normal");
	TabRightNormal=ResMgr->LoadScale9R(L"Tab右_Normal");
	TabCenterNormal=ResMgr->LoadScale9R(L"Tab中_Normal");
	TabFullPush=ResMgr->LoadScale9R(L"Tab全_Push");
	TabLeftPush=ResMgr->LoadScale9R(L"Tab左_Push");
	TabRightPush=ResMgr->LoadScale9R(L"Tab右_Push");
	TabCenterPush=ResMgr->LoadScale9R(L"Tab中_Push");
}

CHTabView::~CHTabView()
{
	ClearItem();
	ResMgr->ReleaseScale9R(TabFullNormal);
	ResMgr->ReleaseScale9R(TabLeftNormal);
	ResMgr->ReleaseScale9R(TabRightNormal);
	ResMgr->ReleaseScale9R(TabCenterNormal);
	ResMgr->ReleaseScale9R(TabFullPush);
	ResMgr->ReleaseScale9R(TabLeftPush);
	ResMgr->ReleaseScale9R(TabRightPush);
	ResMgr->ReleaseScale9R(TabCenterPush);
	ResMgr->ReleaseFontR(FontText);
	delete[] Name;
}

void CHTabView::Draw()
{
	if (TabArray.size()==0)
	{
		D2dMgr->SetColor(0xFF9ECFFF);
		D2dMgr->PushClip(0,0,Width,TabHeight/2);
		D2dMgr->DrawRoundRect(0,0,Width,Height,3);
		D2dMgr->PopClip();
		D2dMgr->PushClip(0,TabHeight/2,Width,TabHeight/2);
		D2dMgr->DrawRectangle(0,0,Width,Height);
		D2dMgr->PopClip();
	}
	else if (TabArray.size()==1)
	{
		D2dMgr->SetImageOpacity(1.0f);
		D2dMgr->DrawImage(0,0,Width,TabHeight,TabFullPush);

        int TextWidth=D2dMgr->GetTextWidth(FontText,TabArray[0]->Title);
		int TextHeight=D2dMgr->GetFontHeight(FontText);
		int TextX=Width/2-TextWidth/2;
		int TextY=TabHeight/2-TextHeight/2;
		D2dMgr->SetColor(0xFF404040);
		D2dMgr->DrawTextWithBorder(TabArray[0]->Title,-1,TextX,TextY,0x80F5A900,FontText);
	}
	else
	{
		int TabWidth=Width/TabArray.size();
		ImageR_t* ImgDraw;
		for (int i=0;i<TabArray.size();i++)
		{
			if (i==0)
			{
				if (i==DisplayIndex)
					ImgDraw=TabLeftPush;
				else
					ImgDraw=TabLeftNormal;
			}
			else if (i==TabArray.size()-1)
			{
				if (i==DisplayIndex)
					ImgDraw=TabRightPush;
				else
					ImgDraw=TabRightNormal;
			}
			else
			{
				if (i==DisplayIndex)
					ImgDraw=TabCenterPush;
				else
					ImgDraw=TabCenterNormal;
			}
			D2dMgr->SetImageOpacity(1.0f);
			D2dMgr->DrawImage(i*TabWidth,0,TabWidth,TabHeight,ImgDraw);

			int TextWidth=D2dMgr->GetTextWidth(FontText,TabArray[i]->Title);
			int TextHeight=D2dMgr->GetFontHeight(FontText);
			int TextX=i*TabWidth+TabWidth/2-TextWidth/2;
			int TextY=TabHeight/2-TextHeight/2;
			D2dMgr->SetColor(0xFF404040);
			D2dMgr->DrawTextWithBorder(TabArray[i]->Title,-1,TextX,TextY,0x80F5A900,FontText);
		}
	}

#ifdef _DEBUG
	D2dMgr->SetColor(0xFF000000);
	D2dMgr->DrawRectangle(0,TabHeight,Width,BodyHeight);
#endif
}

int CHTabView::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (TabArray.size()==0 || DisplayIndex==-1)
		return 0;

	int MouseX=(int)Param1;
	int MouseY=(int)Param2;
	if (UIEvent==EvtMouseDown)
	{
		if (PointInRect(MouseX,MouseY,0,0,Width,TabHeight))
		{
			PushedForClick=TRUE;
			PushX=MouseX;
			PushY=MouseY;
		}
	}
	else if (UIEvent==EvtMouseUp)
	{
		if (PushedForClick)
		{
			if (PointInRect(MouseX,MouseY,0,0,Width,TabHeight))
			{
				int TabWidth=Width/TabArray.size();
				int Index1=PushX/TabWidth;
				int Index2=MouseX/TabWidth;
				if (Index1==Index2)
					SetDisplayTab(Index2);
			}
		}
		PushedForClick=FALSE;
	}
	return 0;
}

int CHTabView::InsertTabPage(UIControl* TabPage,WCHAR* Title,int InsertIndex)
{
	if (InsertIndex==-1)
		InsertIndex=TabArray.size();
	if (InsertIndex>=0 && InsertIndex<=TabArray.size())
	{
		TabItem* NewItem=new TabItem;
		NewItem->Page=TabPage;
		NewItem->Title=wstrdup(Title);
		if (InsertIndex==TabArray.size())
			TabArray.push_back(NewItem);
		else
			TabArray.insert(TabArray.begin()+InsertIndex,NewItem);
		if (TabArray.size()==1)
		{
			SetDisplayTab(0);
		}
		else
		{
			if (InsertIndex<=DisplayIndex)
				DisplayIndex++;
			TabPage->Visible=FALSE;
		}
		TabPage->Y=TabHeight;
		UiMgr->InsertControl(UiMgr->GetControlPageId(this),this,TabPage);
	}
	return InsertIndex;
}

int CHTabView::AppendTabPage(UIControl* TabPage,WCHAR* Title)
{
	TabItem* NewItem=new TabItem;
	NewItem->Page=TabPage;
	NewItem->Title=wstrdup(Title);
	TabArray.push_back(NewItem);
	if (TabArray.size()==1)
		SetDisplayTab(0);
	else
		TabPage->Visible=FALSE;
	TabPage->Y=TabHeight;
	UiMgr->InsertControl(UiMgr->GetControlPageId(this),this,TabPage);
	return TabArray.size()-1;
}

void CHTabView::RemoveTabPage(int Index)
{
	if (Index>=0 && Index<TabArray.size())
	{
		TabItem* Item=*(TabArray.begin()+Index);
		UiMgr->DeleteControl(UiMgr->GetControlPageId(this),Item->Page->Name);
		TabArray.erase(TabArray.begin()+Index);
		delete[] Item->Title;
		delete Item;
		if (TabArray.size()==0)
		{
			DisplayIndex=-1;
		}
		else if (Index==DisplayIndex)
		{
			DisplayIndex--;
			SetDisplayTab(DisplayIndex);
		}
		else
		{
			if (Index>DisplayIndex)
				DisplayIndex--;
		}
	}
}

void CHTabView::ClearItem()
{
	if (this->ChildList.size()==0)
	{
		for (int i=0;i<TabArray.size();i++)
		{
			TabItem* Item=TabArray[i];
			delete[] Item->Title;
			delete Item;
		}
		TabArray.clear();
		DisplayIndex=-1;
	}
	else
	{
		int MgrPageId=UiMgr->GetControlPageId(this);
		for (int i=0;i<TabArray.size();i++)
		{
			TabItem* Item=TabArray[i];
			UiMgr->DeleteControl(MgrPageId,Item->Page->Name);
			delete[] Item->Title;
			delete Item;
		}
		TabArray.clear();
		DisplayIndex=-1;
	}
}

void CHTabView::SetDisplayTab(int Index)
{
	if (Index==DisplayIndex)
		return ;
	if (Index>=0 && Index<TabArray.size())
	{
		TabItem* Current;
		if (DisplayIndex!=-1)
		{
			Current=*(TabArray.begin()+DisplayIndex);
			Current->Page->Visible=FALSE;
		}
		DisplayIndex=Index;
		Current=*(TabArray.begin()+DisplayIndex);
		Current->Page->Visible=TRUE;
	}
}

void CHTabView::SetTabTitle(int Index,WCHAR* Title)
{
	if (Index>=0 && Index<TabArray.size())
	{
		TabItem* Item=*(TabArray.begin()+Index);
		delete[] Item->Title;
		Item->Title=wstrdup(Title);
	}
}